﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Linq.Expressions;
using Bzure.Common.Core;

namespace Bzure.Common.Data
{
    public abstract class EntityRequest
    {

        public enum Operator
        {
            /// <summary>
            /// 等于
            /// </summary>
            Equal = 1,//等于
            /// <summary>
            /// 小于
            /// </summary>
            Lessthan = 2,//小于
            /// <summary>
            /// 大于
            /// </summary>
            Than = 4,//大于
            /// <summary>
            /// 小于等于
            /// </summary>
            LessthanAndEqual = 8,//小于等于
            /// <summary>
            /// 大于等于
            /// </summary>
            ThanAndEqual = 16,//大于等于
            /// <summary>
            /// 左包含
            /// </summary>
            StartWith = 32,//左包含
            /// <summary>
            /// 右包含
            /// </summary>
            EndWith = 64,//右包含
            /// <summary>
            /// 包含
            /// </summary>
            Contain = 128,//包含
            /// <summary>
            /// 不等于
            /// </summary>
            NotEqual=256,
            /// <summary>
            /// 位域包含
            /// </summary>
            BitFlagContain=512,
            /// <summary>
            /// 位域不包含
            /// </summary>
            BitFlagNotContain = 1024
        }
        public enum LogicSymbol
        {
            /// <summary>
            /// 与
            /// </summary>
            And = 1,
            /// <summary>
            /// 或
            /// </summary>
            Or = 2

        }
        public enum LogicLink
        {
            /// <summary>
            /// 左括号
            /// </summary>
            LeftBracket = 1,
            /// <summary>
            /// 右括号
            /// </summary>
            RightBracket = 2

        }

    }
    
    public interface IDataMemberMap
    {
    }
    public interface IMemberDataMap
    {
    }
    public interface IEntityMemberMap
    {
    }
    public class LogicLinkMap : IEntityMemberMap, IDataMemberMap
    {
        public LogicLinkMap()
        {
            LogicLink = EntityRequest.LogicLink.LeftBracket;
            LogicSymbol = EntityRequest.LogicSymbol.And;
        }
        public EntityRequest.LogicLink LogicLink { get; set; }
        public EntityRequest.LogicSymbol LogicSymbol { get; set; }
    }
    /// <summary>
    /// 实体成员映射
    /// </summary>
    public class EntityMemberMap : IEntityMemberMap
    {
        public EntityMemberMap()
        {
            this.Operator = EntityRequest.Operator.Equal;
            this.LogicSymbol = EntityRequest.LogicSymbol.And;
        }
        /// <summary>
        /// 实体成员名
        /// </summary>
        public string MemberName { get; set; }
        /// <summary>
        /// 实体成员值
        /// </summary>
        public object MemberValue { get; set; }
        /// <summary>
        /// 运算符
        /// </summary>
        public EntityRequest.Operator Operator { get; set; }
        /// <summary>
        /// 逻辑运算符
        /// </summary>
        public EntityRequest.LogicSymbol LogicSymbol { get; set; }
    }
    /// <summary>
    /// 实体成员数据映射
    /// </summary>
    public class MemberDataMap : IMemberDataMap
    {
        public MemberDataMap()
        {
            this.Operator = EntityRequest.Operator.Equal;
            //this.LogicSymbol = EntityRequest.LogicSymbol.And;
        }
        /// <summary>
        /// 实体成员名
        /// </summary>
        public string MemberName { get; set; }
        /// <summary>
        /// 数据字段名
        /// </summary>
        public string DataFieldName { get; set; }
        /// <summary>
        /// 运算符
        /// </summary>
        public EntityRequest.Operator Operator { get; set; }
        ///// <summary>
        ///// 逻辑运算符
        ///// </summary>
        //public EntityRequest.LogicSymbol LogicSymbol { get; set; }
        public MemberDataMap Copy()
        {
            return new MemberDataMap()
            {
                DataFieldName = this.DataFieldName,
                MemberName = this.MemberName,
                //LogicSymbol = this.LogicSymbol,
                Operator = this.Operator
            };
        }
    }
    /// <summary>
    /// 数据成员映射
    /// </summary>
    public class DataMemberMap : IDataMemberMap
    {
        public DataMemberMap()
        {
            this.Operator = EntityRequest.Operator.Equal;
            this.LogicSymbol = EntityRequest.LogicSymbol.And;
        }
        /// <summary>
        /// 数据字段名
        /// </summary>
        public string DataFieldName { get; set; }
        /// <summary>
        /// 参数名
        /// </summary>
        public string ParameterName { get; set; }
        /// <summary>
        /// 数据值
        /// </summary>
        public object DataValue { get; set; }
        /// <summary>
        /// 运算符
        /// </summary>
        public EntityRequest.Operator Operator { get; set; }
        /// <summary>
        /// 逻辑运算符
        /// </summary>
        public EntityRequest.LogicSymbol LogicSymbol { get; set; }
    }
    public static class MemberHelper
    {
        
        public static string GetName<T>(Expression<Func<T>> e)
        {
            var member = (MemberExpression)e.Body;
            return member.Member.Name;
        }
        public static EntityMemberMap MemberMap2(object entity, PropertyInfo Member, EntityRequest.Operator Operator = EntityRequest.Operator.Equal, EntityRequest.LogicSymbol LogicSymbol = EntityRequest.LogicSymbol.And)
        {
            return new EntityMemberMap()
            {
                MemberName =Member .Name ,
                MemberValue = Member .GetValue(entity,null),
                Operator = Operator,
                LogicSymbol = LogicSymbol
            };
        }

        public static EntityMemberMap MemberMap<T>(object MemberValue, Expression<Func<T>> e, EntityRequest.Operator Operator = EntityRequest.Operator.Equal, EntityRequest.LogicSymbol LogicSymbol = EntityRequest.LogicSymbol.And)
        {
            return new EntityMemberMap()
            {
                MemberName = GetName(e),
                MemberValue = MemberValue,
                Operator = Operator,
                LogicSymbol = LogicSymbol
            };
        }
        public static MemberDataMap DataMap<T>(string DataFieldName, Expression<Func<T>> e, EntityRequest.Operator Operator = EntityRequest.Operator.Equal)
        {
            return new MemberDataMap()
            {
                MemberName = GetName(e),
                DataFieldName = DataFieldName,
                Operator = Operator
            };
        }
        public static void AppendMember(this List<IDataMemberMap> DataMemberMapSet, MemberDataMap MemberDataMap, EntityMemberMap EntityMemberMap)
        {
            DataMemberMap DataMemberMap = new DataMemberMap()
                                  {
                                      ParameterName = MemberDataMap.DataFieldName +Guid.NewGuid().ToString("N"),
                                      DataFieldName = MemberDataMap.DataFieldName,
                                      DataValue = EntityMemberMap.MemberValue,
                                      Operator = EntityMemberMap.Operator,
                                      LogicSymbol = EntityMemberMap.LogicSymbol
                                  };
            DataMemberMapSet.Add(DataMemberMap);
        }
    }
}
